package com.hassean.we.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.common.util.Query;
import com.hassean.we.admin.common.util.R;
import com.hassean.we.admin.common.web.BaseController;
import com.hassean.we.admin.model.entity.TbRescue;
import com.hassean.we.admin.service.TbRescueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yajun
 * @since 2019-03-29
 */
@RestController
@RequestMapping("/tbRescue")
public class TbRescueController extends BaseController {
    @Autowired private TbRescueService tbRescueService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return TbRescue
    */
    @GetMapping("/{id}")
    public R<TbRescue> get(@PathVariable Long id) {
        return new R<>(tbRescueService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return tbRescueService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  tbRescue  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody TbRescue tbRescue) {
        return new R<>(tbRescueService.insert(tbRescue));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        TbRescue tbRescue = new TbRescue();
        tbRescue.setRescueId(id);
        tbRescue.setUpdateTime(new Date());
        tbRescue.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(tbRescueService.updateById(tbRescue));
    }

    /**
     * 编辑
     * @param  tbRescue  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody TbRescue tbRescue) {
        tbRescue.setUpdateTime(new Date());
        return new R<>(tbRescueService.updateById(tbRescue));
    }
}
